﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZeroBug.EasyFlow.Motion;

namespace Kimd.DMCEtherCard
{
    public class DMCEtherCardDigitalOperator : IDigitalIO
    {
        private bool _isErr;
        private string _lastError;
        public bool IsError { get; set; }

        public string LastError { get; set; }

        public bool CheckApiResult(short rtn, string errMessage)
        {
            if (rtn != 0)
            {
                //_isApiErr = true;
                //MachineMessage.CallException(ExceptionType.FatalErr, "轴卡", errString, MachineErrCode.CardErr);
                //LogHelper.Error("运动控制卡", err, null);
                return false;
            }
            return true;
        }

        public void ClearOutBitStatus(short cardNo, short bitNo)
        {
            CheckApiResult(csLTDMC.LTDMC.dmc_write_outbit((ushort)cardNo, (ushort)bitNo, (ushort)1), "设置通用输出失败!");
        }

        public bool GetInBitStatus(short cardNo, short bitNo)
        {
            //bool[] sts = GetInPortStatus(cardNo);
            //return sts[bitNo];
            short status = 1;
            status = csLTDMC.LTDMC.dmc_read_inbit((ushort)cardNo, (ushort)bitNo);
            return status == 0 ? true : false;
        }

        public bool[] GetInPortStatus(short cardNo)
        {
            throw new NotImplementedException();
        }

        public bool GetOutBitStatus(short cardNo, short bitNo)
        {
            short status = 1;
            status = csLTDMC.LTDMC.dmc_read_outbit((ushort)cardNo, (ushort)bitNo);
            return status == 0 ? true : false;
        }

        public bool[] GetOutPortStatus(short cardNo)
        {
            throw new NotImplementedException();
        }

        public void SetOutBitStatus(short cardNo, short bitNo)
        {
            CheckApiResult(csLTDMC.LTDMC.dmc_write_outbit((ushort)cardNo, (ushort)bitNo, (ushort)0), "设置通用输出失败!");
        }
    }
}
